<?php
use \GatewayWorker\Lib\Gateway;
use Workerman\Lib\Timer;
use \Core\ServiceMessage;
use \Core\ApiMessage;
use \Core\SystemMessage;
class Events{
    // 进程启动时设置个定时器。Events中支持onWorkerStart需要Gateway版本>=2.0.4
    public static function onWorkerStart()
    {
//        Timer::add(1, function(){
//            file_get_contents('http://appapi.oushengvip.cn/ordersync/quqian_user');
//        });
    }




    /**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     *
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        $result['client_id'] = $client_id;
        $data['msg']='链接成功';
        $data['data']=$result;
        $data['type']="connect";
        Gateway::sendToClient($client_id,json_encode($data));
    }


   public static function onMessage($client_id, $message){
        $message_data = json_decode($message, true);
        switch ($message_data['type']){
            case 'init':
                Gateway::bindUid($client_id,$message_data['data']['shop_id']);
                Gateway::sendToUid($message_data['data']['shop_id'],$message);
                break;
            case 'api_init':
                Gateway::bindUid($client_id,$message_data['data']['user_id']);
                Gateway::sendToUid($message_data['data']['user_id'],$message);
                break;
            case 'friends':
                $get_friends_list = ServiceMessage::get_friends($message_data['data']['shop_id']);
                Gateway::sendToUid($message_data['data']['shop_id'],$get_friends_list);
                break;
            case 'message_list':
                $message_list = ServiceMessage::message_list($message_data['data']);
                Gateway::sendToUid($message_data['data']['shop_id'],$message_list);
                break;
            case 'search':
                ServiceMessage::search_money($client_id,$message);
                break;
            case 'api_ping':
                Gateway::sendToClient($client_id,json_encode(['type'=>'ping']));
                break;
            case 'api_message_list':
                $message_list = ApiMessage::message_list($message_data['data']);
                Gateway::sendToUid($message_data['data']['user_id'],$message_list);
                break;
            case 'api_message':
                $insert_message = ApiMessage::insert_message($message_data['data']);
                if($insert_message){
                    $result = $message_data['data'];
                    if(Gateway::isUidOnline($result['user_id'])){
                        Gateway::sendToUid($result['user_id'],$insert_message);
                    }
                    if(Gateway::isUidOnline($result['shop_id'])){
                        Gateway::sendToUid($result['shop_id'],$insert_message);
                    }
                }
                break;
            case 'api_friends':
                $get_friends_list = ApiMessage::get_friends($message_data['data']['shop_id']);
                Gateway::sendToUid($message_data['data']['shop_id'],$get_friends_list);
                break;
            case 'system_users':
                $get_friends_list = SystemMessage::system_users();
                Gateway::sendToUid($message_data['data']['shop_id'],$get_friends_list);
                break;
            case 'system_message_list':
                $message_list = SystemMessage::message_list($message_data['data']);
                Gateway::sendToUid($message_data['data']['shop_id'],$message_list);
                break;
        }
   }
   public static function onClose($client_id){
        var_dump($client_id);
   }
}